package com.example.administrator.x3picturetransliteration.Home.Presenter.Fragment;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Base.HttpGo;
import com.example.administrator.x3picturetransliteration.Bean.AppChannelBean;
import com.example.administrator.x3picturetransliteration.Bean.HttpDataBean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2019/9/3.
 */

public class NavigationPresenter extends BasePresenter{

    public NavigationPresenter(Context context, CompositeSubscription mCompositeSubscription) {
        super(context, mCompositeSubscription);
    }

    /** 首页轮播图 */
    public void carouselList(final Callback mCallback){

        Map<String,String> map = new HashMap<>();

        mCompositeSubscription.add(

                mHttpGo.postSubscription("app_content/carouselList", map, null,new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null){

                                try {

                                    mCallback.getData(httpDataBean.getData());

                                }catch (Exception e){

                                }

                            }
                        }
                    }
                })
        );
    }

    /** 上传图片识别 */
    public void document(List<String> url, final Callback mCallback){

        List<File> httpdata = new ArrayList<>();

        for (String path:url)httpdata.add(new File(path));

        mCompositeSubscription.add(

                mHttpGo.fileSubscription("xfyun/recognize_document", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null)mCallback.getData(httpDataBean.getData());
                        }
                    }
                })
        );

    }

    /** 点击广告图统计 */
    public void Statistics(String type){

        Map<String,String> map = new HashMap<>();
        map.put("type",type);

        mCompositeSubscription.add(

                mHttpGo.postSubscription("data_statistic/add", map,null, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                    }
                })
        );

    }

    /** 获取渠道号 */
    public void app_channel(final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("app_channel/get", new HashMap<String, String>(),null, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getData()!=null)mCallback.getData(JSON.parseObject(httpDataBean.getData() + "", new TypeReference<AppChannelBean>() {}));
                    }
                })
        );

    }

}
